package com.wiz.vo.to.xunlian;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;


/**
 * 讯联提交的数据
 */
public class XlPayResData extends XlBaseData {
	private static final long serialVersionUID = -3465228251115528418L;
	
	// 每个字段具体的意思请查看API文档
	private String txndir;// 交易方向,Q:请求;A:应答
	private String busicd;// 交易类型;PURC：下单支付，PAUT：预下单，INQY：查询订单，VOID：撤销，REFD：退款，VERI：卡券核销
	private String respcd;// 应答码，00:交易成功，09：处理中（需要用户在手机客户端输入支付密码），其他参考应答码表
	private String inscd;// 机构号，商户所属机构标识
	private String chcd;// ALP：支付宝，WXP：微信
	private String mchntid;// 商户号
	private String terminalid;// 终端号
	private String txamt;// 订单金额,12定长字符，单位为分，左补0
	private String channelOrderNum;// 渠道交易号;支付宝/微信返回的订单号
	private String consumerAccount;// 用户账号，支付宝返回支付宝账号，微信支付返回用户OpenID
	private String consumerId;// 用户id，支付宝返回的用户标识，微信不返回
	private String errorDetail;// 交易状态/错误详情
	private String orderNum;// 支付方的订单号
	private String origOrderNum;// 支付方的原交易的订单号
	private String qrcode;// 预下单支付宝返回的二维码url串
	private String scanCodeId;// 客户端软件中展示的条码值，扫码设备扫描获取，即条码
	private String sign;// 签名
	private String chcdDiscount;// 渠道优惠
	private String merDiscount;// 商户优惠
	private String cardId;// 卡券类型
	private String cardInfo;// 卡券详情
	private String currency;// 符合ISO 4217标准的三位币种代码
	private String backUrl;// 异步回调地址

	/**
	 * 	a.对收到JSON响应报文的每个参数（除sign参数外）按照字段名的 ASCII 码从小到大排序后（字典序），使用URL键值对的格式（即 key1=value1&key2=value2…）拼接成字符串 string1。
		b.在 string1 最后直接拼接双方约定的签名密钥K1(接入时讯联侧分配)，得到 stringSignTemp 字符串,并对 stringSignTemp 进行 SHA1 运算，得到验签字段 sign1
		c.对比响应报文中 sign 参数的值和上面得到的 sign1，如果相等则验证通过，若不等，则验签不通过。
	 */
	public String toSign(String key){
		Map<String,Object> map = new HashMap<String, Object>();
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            Object obj;
            try {
                obj = field.get(this);
                if(obj!=null){
                    map.put(field.getName(), obj);
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        ArrayList<String> list = new ArrayList<String>();
        for(Map.Entry<String,Object> entry:map.entrySet()){
            if(entry.getValue()!=""){
                list.add(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }
        int size = list.size();
        String [] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < size; i ++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        result += key;
        return DigestUtils.shaHex(result);
	}
	
	public String getTxndir() {
		return txndir;
	}

	public void setTxndir(String txndir) {
		this.txndir = txndir;
	}

	public String getBusicd() {
		return busicd;
	}

	public void setBusicd(String busicd) {
		this.busicd = busicd;
	}

	public String getRespcd() {
		return respcd;
	}

	public void setRespcd(String respcd) {
		this.respcd = respcd;
	}

	public String getInscd() {
		return inscd;
	}

	public void setInscd(String inscd) {
		this.inscd = inscd;
	}

	public String getChcd() {
		return chcd;
	}

	public void setChcd(String chcd) {
		this.chcd = chcd;
	}

	public String getMchntid() {
		return mchntid;
	}

	public void setMchntid(String mchntid) {
		this.mchntid = mchntid;
	}

	public String getTerminalid() {
		return terminalid;
	}

	public void setTerminalid(String terminalid) {
		this.terminalid = terminalid;
	}

	public String getTxamt() {
		return txamt;
	}

	public void setTxamt(String txamt) {
		this.txamt = txamt;
	}

	public String getChannelOrderNum() {
		return channelOrderNum;
	}

	public void setChannelOrderNum(String channelOrderNum) {
		this.channelOrderNum = channelOrderNum;
	}

	public String getConsumerAccount() {
		return consumerAccount;
	}

	public void setConsumerAccount(String consumerAccount) {
		this.consumerAccount = consumerAccount;
	}

	public String getConsumerId() {
		return consumerId;
	}

	public void setConsumerId(String consumerId) {
		this.consumerId = consumerId;
	}

	public String getErrorDetail() {
		return errorDetail;
	}

	public void setErrorDetail(String errorDetail) {
		this.errorDetail = errorDetail;
	}

	public String getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(String orderNum) {
		this.orderNum = orderNum;
	}

	public String getOrigOrderNum() {
		return origOrderNum;
	}

	public void setOrigOrderNum(String origOrderNum) {
		this.origOrderNum = origOrderNum;
	}

	public String getQrcode() {
		return qrcode;
	}

	public void setQrcode(String qrcode) {
		this.qrcode = qrcode;
	}

	public String getScanCodeId() {
		return scanCodeId;
	}

	public void setScanCodeId(String scanCodeId) {
		this.scanCodeId = scanCodeId;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getChcdDiscount() {
		return chcdDiscount;
	}

	public void setChcdDiscount(String chcdDiscount) {
		this.chcdDiscount = chcdDiscount;
	}

	public String getMerDiscount() {
		return merDiscount;
	}

	public void setMerDiscount(String merDiscount) {
		this.merDiscount = merDiscount;
	}

	public String getCardId() {
		return cardId;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

	public String getCardInfo() {
		return cardInfo;
	}

	public void setCardInfo(String cardInfo) {
		this.cardInfo = cardInfo;
	}

	public String getCurrency() {
		return currency;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}

	public String getBackUrl() {
		return backUrl;
	}

	public void setBackUrl(String backUrl) {
		this.backUrl = backUrl;
	}

}
